const app = getApp()
const api = require('../../utils/api.js');
const config = require('../../config/config.js');
var {request} = require('../../utils/api.js')
// const util = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imagePath:"",
    case_lst:[],
    type_lst:[],
    provinceList: [],
    activeId:app.globalData.activeId || null,
    activePId: null,
    activeCId: null,
    cityList: '',
    pCode: '',
    searchValue: ""
},
    /**
    * @desc : 组装数据
    * @author : 陈浩然
    * @date : 2024年9月21日09:08:45
    */
   getData() {
    this.setData({
      loading: true
    })
    
    if(app.globalData.activeId){
      this.setData({
        activeId: app.globalData.activeId
      })
    }
    api.request('case_lst_page', 'POST',{
      type_id:this.data.activeId,
      p_id: this.data.activePId,
      c_id: this.data.activeCId
    }).then(res => {
      let data = res.data.data
      this.setData({
        case_lst:data.info_lst,
        type_lst:data.type_lst,
        imagePath: config.server_img_http
      })
    })
  },

  // 跳转
  toPages(e){
    wx.navigateTo({
      url: '/pages/case/case-info/case-info?id='+e.currentTarget.dataset.id,
      fail: function(res) {
        console.log(res)
      }
    });
  },

  selectProvince(){
    request('province', 'POST').then(res => {
      if (res.data.code == 200) {
        this.setData({
          provinceList:res.data.data
        })
      }
    })
  },
  
  selectCity(){
    let params={
      pid:this.data.pCode
    }
    request('city','post',params).then(res => {
      if (res.data.code == 200) {
        this.setData({
          cityList:res.data.data
        })
      }
    })
  },
  select(e) {
    console.log(e)
    let id = e.currentTarget.dataset.id
    let type = e.currentTarget.dataset.type
    let code = e.currentTarget.dataset.code
    if (type == 3) {
      app.globalData.activeId = id
      this.setData({
        activeId: id
      })
    }
    if (type == 4) {
      this.setData({
        activePId: id,
        pCode: code
      })
      this.selectCity()
    }
    if (type == 5) {
      this.setData({
        activeCId: id
      })
    }
    this.getData()
  },

    // 修改搜索文字
    onChangeSearchValue(e){
      console.log(e)
      this.setData({
        searchValue: e.detail
      })
    },
    // 搜索
    search(e){
      this.setData({
        loading: true
      })
      let data = {
        condition: this.data.searchValue,
        type_id:this.data.activeId,
        p_id: this.data.activePId,
        c_id: this.data.activeCId
      }
      
      api.request('case_lst_page?condition='+ this.data.searchValue + '&type_id=' + this.data.activeId + '&p_id=' + this.data.activePId + '&c_id=' + this.data.activeCId, 'POST').then(res => {
        console.log(res)
        let data = res.data.data
        this.setData({
          case_lst:data.info_lst,
          type_lst:data.type_lst,
          imagePath: config.server_img_http
        })
      })
    },


  clearTitle(){
    let id = null
    app.globalData.activeId = id
    this.setData({
      activeId: id
    })
    this.getData()
  },
  clearProvinceTitle() {
    this.setData({
      activePId: null,
      cityList: [],
      activeCId: null
    })
    this.getData()
  },
  clearCityTitle() {
    this.setData({
      activeCId: null
    })
    this.getData()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow(options) {
    this.getData()
    this.selectProvince()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})